<template>
  <div class="main">
    <div class="info-detail-wrapper">
      <div v-if="showBackArrow">
        <header-back>
          <img @btnclick="shareOpen" v-hide class="my-right" slot="my-right" src="~@/static/images/shareBlack.png" alt="" />
        </header-back>
      </div>
      <scroller ref="scrollerBottom" :on-infinite="handleReachButton" noDataText="没有更多评论">
        <div v-show="showPage">
          <div style="height:0.88rem" v-if="showBackArrow"></div>
          <div id="info-detail-box">
            <div class="header">
              <span class="spn1">
                <b>{{ headerTitle }}</b>
              </span>
              <span class="spn2 flex-h">
                <b class="flex1">{{ inTime }}</b>
                <em class="bg-password_icon_eye_s icons"></em>
                <i>{{ viewCount }}</i>
              </span>
            </div>
            <div v-html="dataDetail" class="vHtml" id="vHtml"></div>
            <div class="botBox">
              <span class="spn3"> </span>
              <span :class="['spn4', sp == true ? 'spnActive' : '']" @click="ssr11">
                <i></i>
                <b>{{ sp == true ? likeCount + 1 : likeCount }}</b>
              </span>
            </div>
            <div class="biaobianBox">
              <span v-for="(item, index) in labelsArr" :key="index" @click="commonJs.gopush('InfoLabelList', item.id)">{{ item.name }}<i v-show="index < labelsArr.length - 1">·</i></span>
            </div>
          </div>
          <div class="tuijian">
            <h4>推荐新闻</h4>
            <info-list :newsData="tujianList" v-on:listenToNews="goes"></info-list>
          </div>
          <div class="hotResponse" v-show="showComment">
            <div class="resTitle">
              <div class="left">热门评论</div>
              <div class="right" @click="writeComment">写评论</div>
            </div>
            <ul class="responseList">
              <li class="resItem" v-for="(item, index) of topicList" :key="index">
                <div class="middle_img">
                  <img :src="item.author.avatar ? item.author.avatar : defaultAvatar" />
                </div>
                <div class="content">
                  <div class="content_title">
                    <div class="title_1">
                      <div class="header">
                        <div class="name">{{ item.author.name }}</div>
                      </div>
                      <p>{{ item.createTime ? new Date(item.createTime).format('yyyy-MM-dd hh:mm') : '' }}</p>
                    </div>
                  </div>
                  <div class="suggestion">{{ item.content }}</div>
                </div>
              </li>
            </ul>
          </div>
        </div>
      </scroller>
    </div>
  </div>
</template>

<script>
import { topicInfo, topicCommentPage } from '~/api/identity'
import infoList from '~/components/infoList'
import headerBack from '~/components/headerBack'
import ls from 'store2'
import { format } from '~/plugins/filter'
import { showShare } from '~/components/share/index'
import wxapi from '~/plugins/wxapi.js'
export default {
  // head: {
  //   title: "详情"
  // },
  components: {
    infoList,
    headerBack,
  },
  data() {
    return {
      showBackArrow: true,
      showComment: true,
      showPage: false,
      currentPage: 1,
      topicList: [],
      defaultAvatar: require('../../static/usermoren.png'), // 默认头像
      dataDetail: '',
      headerTitle: '',
      pageCount: 0,
      classes: '',
      viewCount: '10k',
      likeCount: 30,
      inTime: 1535338974000,
      tujianList: [],
      sp: false,
      labelsArr: [],
    }
  },
  created() {
    if (this.commonJs.GetUrlParam('shareInfo')) {
      this.showBackArrow = false
      this.showComment = false
    }
  },
  mounted: function() {
    this.$visit('News/' + this.commonJs.GetUrlParam('type'))
    wxapi.wxRegister()
    this.showDetails()
    this.topicCommentPage()
    if (this.commonJs.GetUrlParam('showComment')) {
      this.showComment = false
    }
    if (sessionStorage.getItem('fromplat')) {
      this.showComment = false
    }
  },
  destroyed() {},
  methods: {
    wxShare(pic, titles, descs, id) {
      let uri = window.location.href + '&shareInfo=' + true

      let option = {
        title: titles,
        desc: descs,
        link: uri,
        imgUrl: pic,
      }
      wxapi.ShareAppMessage(option)
      wxapi.ShareTimeline(option)
    },
    shareOpen() {
      showShare('open')
    },
    goes(tid) {
      this.showPage = false
      this.dataDetail = ''
      this.headerTitle = ''
      this.viewCount = ''
      this.classes = ''
      this.tujianList = []
      this.likeCount = ''
      this.inTime = ''
      this.labelsArr = []
      this.currentPage = 1
      this.topicList = []
      this.goto('../info/infoDetail', tid)
      this.showDetails(tid)
      this.topicCommentPage()
    },

    showDetails(id) {
      this.$loading('open')
      this.$refs.scrollerBottom.scrollTo(0, 0, false)
      id ? (this.tid = id) : (this.tid = this.$route.query.type)
      var _this = this
      topicInfo({ tid: this.tid })
        .then((res) => {
          _this.dataDetail = res.data.content
          _this.headerTitle = res.data.title
          _this.viewCount = res.data.viewCount
          res.data.labels.length ? (_this.classes = _this.classes = res.data.labels[0].name) : (_this.classes = '')
          // _this.classes = res.data.labels[0].name;
          _this.tujianList = res.data.relatedReads
          _this.likeCount = res.data.likeCount
          _this.inTime = new Date(res.data.inTime).format('yyyy-MM-dd hh:mm')
          _this.labelsArr = res.data.labels
          this.showPage = true

          this.wxShare(res.data.cover, res.data.title, res.data.description)
          this.$hideLoading()
        })
        .catch((error) => {
          this.$hideLoading()
          this.$vux.toast.text(error)
        })
    },

    ssr11() {
      this.sp = !this.sp
    },
    goto(routerName, queryData) {
      this.$router.replace({
        path: routerName,
        query: {
          type: queryData,
        },
      })
    },

    //pinglun
    topicCommentPage() {
      let obj = {
        pageNo: this.currentPage,
        pageSize: 10,
        topicId: this.$route.query.type,
      }
      topicCommentPage(obj).then((res) => {
        if (res.code == '0') {
          this.totalPages = Math.ceil(res.data.total / res.data.pageSize)
          const arr = res.data.list
          arr.forEach((item) => {
            if (this.topicList.indexOf(item) == -1) {
              this.topicList.push(item)
            }
          })
        }
      })
    },
    handleReachButton(done) {
      if (this.currentPage < this.totalPages) {
        this.currentPage = this.currentPage + 1
        topicCommentPage({
          pageNo: this.currentPage,
          pageSize: 10,
          topicId: this.$route.query.type,
        }).then((res) => {
          if (res.code == '0') {
            let newArr = res.data.list
            this.topicList = this.topicList.concat(newArr)
            this.totalPages = Math.ceil(res.data.total / res.data.pageSize)
            done()
          }
        })
      } else {
        done(true)
        return
      }
    },
    writeComment() {
      this.commonJs.gopush('tiwen', this.$route.query.type)
    },
  },
}
</script>

<style scoped lang="less">
.tuijian {
  padding: 0.3rem 0;
  background: #f7f7fa;
  h4 {
    display: block;
    font-size: 0.36rem;
    font-weight: 500;
    padding-left: 0.2rem;
    color: #666;
    margin-bottom: 0.2rem;
  }
}
._v-container {
  width: 100%;
  height: 100vh;
}

.main {
  width: 100%;
  height: 100vh;
  font-size: 0.3rem;
}

.info-detail-wrapper {
  width: 7.5rem;
  height: 100vh;
  background: #fff;
  .my-right {
    width: 0.4rem;
    height: 0.4rem;
  }
}

#info-detail-box {
  background: #fff;
  box-sizing: border-box;
  width: 7.5rem;
  font-size: 0.3rem;
  margin: 0 auto;
  padding: 0rem 0.3rem 0.5rem;
  box-sizing: border-box;
  .biaobianBox {
    span {
      font-size: 0.28rem;
      display: inline-block;
      border-radius: 3px;
      margin-bottom: 0.2rem;
      color: #999;
      i {
        margin-left: 0.2rem;
        margin-right: 0.2rem;
      }
    }
  }

  .header {
    width: 100%;
    height: auto;
    padding-bottom: 0.3rem;
    border-bottom: 1px solid #b7b7b7;
    margin-bottom: 0.1rem;

    b,
    i {
      font-style: normal;
      font-weight: 500;
    }

    .spn1 {
      display: block;
      width: 100%;
      box-sizing: border-box;

      b {
        font-size: 0.4rem;
        font-family: PingFangSC-Semibold, PingFangSC;
        font-weight: 600;
        color: rgba(34, 34, 34, 1);
        line-height: 0.6rem;
      }
    }

    .spn2 {
      height: 0.5rem;
      overflow: hidden;
      margin-top: 0.3rem;

      b,
      i {
        font-size: 0.28rem;
        color: #b7b7b7;
        display: inline-block;
      }
      .icons {
        vertical-align: middle;
      }
      i {
        vertical-align: middle;
        height: 0.5rem;

        margin-left: 0.08rem;
        line-height: 0.5rem;
      }
    }
  }

  .vHtml {
    width: 100%;
    height: auto;
    font-size: 0.3rem;
    /deep/ img {
      width: 100%;
    }
  }

  p {
    margin: 0 !important;
  }

  #vHtml {
    img {
      display: block;
      width: auto;
      max-width: 100% !important;
    }
  }
  .botBox {
    width: 100%;
    height: 2rem;
    padding-top: 0.3rem;
    box-sizing: border-box;
    overflow: hidden;
    text-align: center;
    span {
      display: block;
      overflow: hidden;
    }

    .spn3 {
      text-align: right;

      b {
        font-weight: 500;
        font-size: 0.26rem;
        color: #bfbfbf;
      }
    }

    .spn4 {
      width: auto;
      height: 0.8rem;
      background: rgba(255, 255, 255, 1);
      box-shadow: 0px 2px 0.1rem 0px rgba(153, 153, 153, 0.2);
      border-radius: 0.4rem;
      padding: 0 0.4rem;

      margin: 0.3rem auto 0;
      text-align: center;
      line-height: 0.8rem;
      display: inline-block;
      b,
      i {
        font-weight: 500;
        font-style: normal;
        font-size: 0.3rem;
        color: #bfbfbf;
        vertical-align: middle;
      }

      i {
        display: inline-block;
        width: 0.4rem;
        height: 0.4rem;
        background: url('~@/static/sprite/other_sprites.png') -1.35rem -3.2rem;
        background-size: 4.68rem 4.08rem;
      }

      b {
        margin-left: 0.05rem;
      }
    }

    .spnActive {
      i {
        background: url('~@/static/sprite/other_sprites.png') -1.95rem -3.2rem;
        background-size: 4.68rem 4.08rem;
      }

      b {
        color: @mainColor;
      }
    }
  }
}
.subtitle {
  padding: 0 0.3rem;
  box-sizing: border-box;
  height: 0.8rem;
  margin: 0.2rem auto;
  span {
    font-size: 0.4rem;
    font-family: PingFangSC;
    font-weight: 600;
    color: rgba(34, 34, 34, 1);
    line-height: 0.8rem;
  }
  img {
    width: 0.6rem;
    height: 0.6rem;
  }
}

//[评论]
.hotResponse {
  padding: 0.56rem 0.32rem 0.8rem;
  background: #fff;

  .resTitle {
    display: flex;
    justify-content: space-between;
    align-items: center;

    .left {
      color: #222;
      font-size: 0.36rem;
      font-weight: bold;
      line-height: 0.4rem;
    }

    .right {
      color: @mainColor;
      font-size: 0.28rem;
      font-weight: bold;
      line-height: 0.4rem;
    }
  }

  .responseList {
    list-style: none;
    padding-left: 0;

    .resItem {
      padding: 0.46rem 0 0.32rem;
      border-bottom: 1px solid #e5e8ed;
      display: flex;
      justify-content: space-between;

      .middle_img {
        text-align: center;
        img {
          width: 0.8rem;
          height: 0.8rem;
          border-radius: 50%;
        }
      }
      .content {
        width: 85%;
        .content_title {
          display: flex;
          justify-content: space-between;
          align-items: center;
          margin-bottom: 0.25rem;
          margin-bottom: 0.25rem;
          .title_1 {
            font-size: 0.32rem;
            width: 100%;
            .header {
              display: flex;
              justify-content: space-between;
              align-items: center;

              .name {
                font-size: 0.3rem;
                color: #222;
                font-weight: bold;
              }

              .right {
                display: flex;
                justify-content: flex-start;
                align-items: center;
                img {
                  width: 0.4rem;
                  height: 0.4rem;
                }
                .third {
                  color: #c7c7c7;
                  font-size: 0.26rem;
                  margin-left: 0.04rem;
                  line-height: 0.3rem;
                }
              }
              .isLiked {
                .third {
                  color: #ffbd3e;
                }
              }
            }

            p {
              font-size: 0.24rem;
              color: #c7c7c7;
              margin-top: 0.06rem;
            }
          }
        }
        .suggestion {
          color: #999;
          margin-bottom: 0.15rem;
          font-size: 0.28rem;
        }
      }
    }
  }
}
</style>
